package starfire.gui.defaultModelAndRenderer;

import java.io.*;
import java.util.*;
import javax.swing.Icon;
import javax.swing.filechooser.FileSystemView;

/**
 * <p>Title: JFtp</p>
 * <p>Description: ftp client</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author starfire
 * @version 1.0
 */

public class IconCollection {
  private static Hashtable iconHash = new Hashtable();
  private static FileSystemView fsv = FileSystemView.getFileSystemView();
  private static String prefix = "jftptemp";

  private IconCollection() {
  }

  public static Icon getIcon(File file)
  {
    String name = file.getName();
    int index = name.lastIndexOf(".");
    String suffix = null;
    if (index==-1)
    {
      suffix = "";
    }
    else
    {
      suffix = name.substring(index);
    }
    if (iconHash.get(suffix)==null)
    {
      if (!(file instanceof starfire.ftp.FtpFile) && file.exists() ){
        iconHash.put(suffix, fsv.getSystemIcon(file));
      }
      else {
        try {
          File temp = File.createTempFile(prefix, suffix);
          iconHash.put(suffix, fsv.getSystemIcon(temp));
          temp.delete();
        }
        catch (Exception ex) {
          ex.printStackTrace();
          System.out.println("can't create temp file");
        }
      }
    }
    return (Icon)iconHash.get(suffix);
  }

}
